/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.codegen.jfinal;

import com.hyts.codegen.common.model.SimpleBuildModel;
import com.hyts.codegen.jfinal.base.BaseJfinalCreator;
import com.hyts.codegen.jfinal.creator.DefaultJfinalCodeCreator;
import com.hyts.guava.String.Strings;
import com.hyts.guava.object.Objects;
import com.hyts.template.common.AbstractFileWriter;
import com.hyts.template.strformat.StringCodeExecutor;

/** 
 * @title JfinalGenerator.java  
 * @package com.hyts.codegen.jfinal  
 * @description TODO  
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月12日 
 * @version V1.0 
 */
public class JfinalGenerator {

	/**  
	 * <p>{字段的描述}</p> 
	 * @fieldName creator
	 * @fieldType BaseJfinalCreator<SimpleBuildModel>
	 */ 
	BaseJfinalCreator<SimpleBuildModel> creator;

	/**  
	 * <p>{字段的描述}</p> 
	 * @fieldName fileWriter
	 * @fieldType AbstractFileWriter<String>
	 */ 
	AbstractFileWriter<String> fileWriter;
	
	/**  
	 * @constructor：JfinalGenerator  
	 * @param creator  
	 */ 
	public JfinalGenerator(BaseJfinalCreator<SimpleBuildModel> creator) {
		super();
		Objects.checkNull(creator);
		this.creator = creator;
	}
	
	public JfinalGenerator() {
		super();
		this.creator = new DefaultJfinalCodeCreator();
		this.fileWriter = new StringCodeExecutor();
	}
	
	/**  
	 * <b>createModelInternal</b>
	 * <p>创建模型成功</p>
	 * @param model
	 * @return
	 * @exception
	 */ 
	public String createModelInternal(SimpleBuildModel model){
		String javaFullPath = model.getBasePackageName().replace(".","/")+"/"+model.getModelPackageName()+"/"+model.getJavaTypeName()+".java";
			//如果采用jfinal原生的代码生成方法
			if(!model.usedJfinalPrototype) {
				model.setAnnotations(new String[]{"Entity(tableName=\""+Strings.upperCamelToUnderLineUpperCase(model.getJavaTypeName())+"\")"});
				model.setImportClassName(new String[]{"import com.hyts.jfmvc.scan.annotation.Entity"});
			}
			return this.fileWriter.writerInternal(javaFullPath, this.creator.buildModel(model));
	}
	
	/**  
	 * <b>createModelExternal</b>
	 * <p>创建模型成功</p>
	 * @param model
	 * @return
	 * @exception
	 */ 
	public String createModelExternal(SimpleBuildModel model){
			//如果采用jfinal原生的代码生成方法
			if(!model.usedJfinalPrototype) {
				model.setAnnotations(new String[]{"Entity(tableName=\""+model.TABLE_MODEL_PREFIX+Strings.upperCamelToUnderLineUpperCase(model.getJavaTypeName())+"\")"});
				model.setImportClassName(new String[]{"import com.hyts.jfmvc.scan.annotation.Entity"});
			}
			return this.fileWriter.writerExternal(model.getExternalFilePath()+model.getBasePackageName().replace(".","/")+"/"+model.getModelPackageName()+"/"+model.getJavaTypeName()+".java",
																			  this.creator.buildModel(model));
	}
	
	/**  
	 * <b>TODO(方法功能的描述)</b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param model
	 * @return
	 * @exception
	 */ 
	public String createDaoInternal(SimpleBuildModel model){
		String javaFullPath = model.getBasePackageName().replace(".","/")+"/"+model.getModelPackageName()+"/"+model.getJavaTypeName()+".java";
			//如果采用jfinal原生的代码生成方法
			if(!model.usedJfinalPrototype) {
				model.setAnnotations(new String[]{"Repository"});
			}
			return this.fileWriter.writerInternal(javaFullPath, this.creator.buildDao(model));
	}

	/**
	 *
	 * @param model
	 * @return
	 */
	public String createDaoExternal(SimpleBuildModel model){
		//如果采用jfinal原生的代码生成方法
		if(!model.usedJfinalPrototype) {
			model.setAnnotations(new String[]{"Repository"});
			model.setImportClassName(new String[]{"import com.hyts.jfmvc.scan.annotation.Repository"});
		}
		return this.fileWriter.writerExternal(model.getExternalFilePath()
				+model.getBasePackageName().replace(".","/")+"/"
				+model.getModelPackageName()+"/"+model.getJavaTypeName()+".java",
				this.creator.buildDao(model));
	}

	/**
	 * @param model
	 * @return
	 * @exception
	 */
	public String createServiceInternal(SimpleBuildModel model){
		String javaFullPath = model.getBasePackageName().replace(".","/")+"/"+model.getModelPackageName()+"/"+model.getJavaTypeName()+".java";
		//如果采用jfinal原生的代码生成方法
		if(!model.usedJfinalPrototype) {
			if(model.isInterface())
				model.setAnnotations(new String[]{});
			else {
				model.setAnnotations(new String[]{"Service"});
			}
		}
		return this.fileWriter.writerInternal(javaFullPath, this.creator.buildService(model));
	}


	/**
	 * @param model
	 * @return
	 * @exception
	 */
	public String createControllerInternal(SimpleBuildModel model,String modelName){
		String javaFullPath = model.getBasePackageName().replace(".","/")+"/"+model.getModelPackageName()+"/"+model.getJavaTypeName()+".java";
		//如果采用jfinal原生的代码生成方法
		if(!model.usedJfinalPrototype) {
			model.setAnnotations(new String[]{String.format("Controller(\"/%s\")",modelName)});
		}
		return this.fileWriter.writerInternal(javaFullPath, this.creator.buildController(model));
	}

	/**
	 *
	 * @param model
	 * @return
	 */
	public String createServiceExternal(SimpleBuildModel model){
		return null;
	}

}	
